package com.gifts.ui.page.common;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * Reusable component class for thumbnail
 * @author dmaung
 *
 */
public class JThumbnail  extends JPanel {
	
	public JThumbnail() {
		
	}
	
	public JThumbnail(String url, String text1) throws MalformedURLException {
		
		setText(url, text1);

	}
	
	
	public JThumbnail(String url, String text1, String text2) throws MalformedURLException {
		
		setText(url, text1, text2);
	
		
		
	}
	
	public JThumbnail(String url, String text1, String text2,String text3) throws MalformedURLException {
		
		setText(url, text1, text2,text3);
	
		
		
	}
	
	public void setText(String url, String text1) throws MalformedURLException{
		removeAll();
		ImageIcon imageIcon = new ImageIcon(getScaledImage(new ImageIcon(new URL(url)).getImage(),80,70));
		
		JLabel label1 = new JLabel(imageIcon);
		label1.setSize(100, 100);
		add(label1);
		text1 = formatText(text1);
		JLabel label2 = new JLabel(text1);
		label2.setSize(100, 100);
		add(label2);
		
		FlowLayout layout = new FlowLayout();
		
		setLayout(layout);
		setPreferredSize(new Dimension(120,120));
		
		
	}
	
	public void setText(String url, String text1, String text2) throws MalformedURLException{
		removeAll();
		ImageIcon imageIcon = new ImageIcon(getScaledImage(new ImageIcon(new URL(url)).getImage(),80,70));
		
		JLabel label1 = new JLabel(imageIcon);
		label1.setSize(100, 100);
		add(label1);
		text1 = formatText(text1);
		JLabel label2 = new JLabel(text1);
		label2.setSize(100, 100);
		add(label2);
		text2 = formatText(text2);
		JLabel label3 = new JLabel(text2);
		label2.setSize(100, 100);
		add(label3);
		FlowLayout layout = new FlowLayout();
		
		setLayout(layout);
		setPreferredSize(new Dimension(120,120));
		
	}
	
	public void setText(String url, String text1, String text2, String text3) throws MalformedURLException{
		removeAll();
		ImageIcon imageIcon = new ImageIcon(getScaledImage(new ImageIcon(new URL(url)).getImage(),80,70));
		
		JLabel label1 = new JLabel(imageIcon);
		label1.setSize(100, 100);
		add(label1);
		text1 = formatText(text1);
		JLabel label2 = new JLabel(text1);
		label2.setSize(120, 100);
		add(label2);
		text2 = formatText(text2);
		JLabel label3 = new JLabel(text2);
		label3.setSize(120, 100);
		add(label3);
		
		text2 = formatText(text3);
		JLabel label4 = new JLabel(text3);
		label4.setSize(120, 100);
		add(label4);
		FlowLayout layout = new FlowLayout();
		
		setLayout(layout);
		setPreferredSize(new Dimension(120,200));
		
	}
	
	 /**
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
    
    private String formatText(String text) {
    	if(text.length() > 18) {
    		text = text.substring(0,15) + "...";
    	}
    	return text;
    }

}
